package com.cskaoyan.controller.backStage;

import com.cskaoyan.model.Role;
import com.cskaoyan.model.bo.RoleBO;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.bo.RolePermissionBO;
import com.cskaoyan.model.vo.*;
import com.cskaoyan.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: mall38
 * @description: 角色管理模块
 * @author: Lexis
 * @create: 2022-05-05 18:59
 **/
@RestController
@RequestMapping("admin/role")
public class RoleController {
    @Autowired
    RoleService roleService;
    @RequiresPermissions("admin:role:options")
    @GetMapping("options")
    /**
     * @author: lixiaosheng
     * @description: 获取所有的role
     * @date: 2022/5/5 19:03
     */
    public BaseRespVO<DataVO<Role>> roleList() {
        BaseParamBO param = new BaseParamBO();
        DataVO<GetRoleVO> roleVO = roleService.query(param);
        return BaseRespVO.ok(roleVO);
    }
    @RequiresPermissions("admin:role:create")
    @PostMapping("create")
    /**
     * @author: lixiaosheng
     * @description: 新增一个角色
     * @date: 2022/5/5 23:32
     * @param roleBO
     */
    public BaseRespVO<Role> create(@RequestBody RoleBO roleBO) {
        Role role = roleService.create(roleBO);
        return BaseRespVO.ok(role);
    }
    @RequiresPermissions("admin:role:list")
    @GetMapping("list")
    /**
     * @author: lixiaosheng
     * @description: 获取所有的角色的完整信息
     * @date: 2022/5/5 23:38
     */
    public BaseRespVO<DataVO<Role>> roleData(BaseParamBO param, String name) {
        DataVO<Role> roleDataVO = roleService.queryRole(param, name);
        return BaseRespVO.ok(roleDataVO);
    }
    @RequiresPermissions("admin:role:update")
    @PostMapping("update")
    /**
     * @author: lixiaosheng
     * @description: 更新角色
     * @date: 2022/5/6 14:25
     */
    public ExceptionVO update(@RequestBody Role role) {
        roleService.update(role);
        return ExceptionVO.fine();
    }
    @RequiresPermissions("admin:role:delete")
    @PostMapping("delete")
    /**
     * @author: lixiaosheng
     * @description: 逻辑删除一个角色，相关管理员中的角色也将删除
     * @date: 2022/5/6 14:45
     */
    public ExceptionVO delete(@RequestBody Role role) {
        roleService.delete(role);
        return ExceptionVO.fine();
    }
    @RequiresPermissions("admin:role:permissions")
    @GetMapping("permissions")
    /**
     * @author: lixiaosheng
     * @description: 显示资源权限表
     * @date: 2022/5/7 22:07
     */
    public BaseRespVO<PermissionDataVO> permissions(String roleId) {
        PermissionDataVO permissionDataVO = roleService.getPermissions(roleId);
        return BaseRespVO.ok(permissionDataVO);
    }
    @RequiresPermissions("admin:role:permissions")
    @PostMapping("permissions")
    /**
     * @author: lixiaosheng
     * @description: 物理更新角色权限
     * @date: 2022/5/8 13:06
     */
    public ExceptionVO permissions(@RequestBody RolePermissionBO permissionBO) {
        roleService.updatePermissions(permissionBO);
        return ExceptionVO.fine();
    }
}
